import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cut: true, //用户动态信息或相册事件
    follreveal: false, //关注弹窗事件
    photoList: [], //用户相册数组
    userInfo: {}, //是否是当前用户
    userId: null,
    UserStatuslist: [], //用户动态数组
    count: null, //保存用户动态总数量
    Hizfs: false, //转发底部弹窗
    Hireveal: false, //操作/底部弹窗事件
    pageNum: 1, //动态列表页码值
    nextPage: null, //判断动态列表是否还有下一页数据
    pagePhotoNum: 1, //照片列表页码值
    nextPhotoPage: null, //判断照片列表是否还有下一页数据

    stateList: [], //用户状态
    deleteId: [], //删除动态
    listId: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: options.id,
    })
    this.getStatuslist()
    this.getRestUser()

    // if (JSON.parse(wx.getStorageInfoSync('deleteId')!== '')) {
    //   let deleteId = JSON.parse(wx.getStorageInfoSync('deleteId'))
    //   this.setData({
    //     deleteId
    //   })
    // }
  },


  //  获取用户动态列表
  getStatuslist() {
    let that = this
    myRequesy({
      url: 'user/default-dynamic',
      data: {
        user_id: that.data.userId,
        page: that.data.pageNum
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          UserStatuslist: res.data.data,
          count: res.data.count,
          nextPage: res.data.next_url
        })
      }
    })
  },



  // 关注弹窗事件
  btnCancel() {
    this.setData({
      follreveal: false
    })
  },

  // 关注或取消关注
  headleAtte() {
    let thst = this
    let {
      userInfo,
      userId
    } = thst.data
    if (userInfo.follow_state !== 0) {
      this.setData({
        follreveal: true
      })
    } else {
      myRequesy({
        url: 'follow/add',
        method: 'POST',
        data: {
          user_id: userId
        },
      }).then(res => {
        if (res.data.code === 0) {
          wx.showToast({
            title: '关注成功',
          })
          this.getRestUser()
        }

      })
    }

  },



  // 用户取消关注
  headleConfirm() {
    let that = this
    myRequesy({
      url: 'follow/take-off',
      method: 'POST',
      data: {
        user_id: that.data.userId
      }
    }).then(res => {
      if (res.data.code === 0) {
        wx.showToast({
          title: '已取消关注',
        })
        that.setData({
          follreveal: false,
        })
        that.getRestUser()
      }
    })
  },
  //获取用户动态相册列表
  getPhotolist() {
    let that = this
    myRequesy({
      url: 'user/album-dynamics',
      data: {
        user_id: that.data.userId,
        page: that.data.pagePhotoNum
      }
    }).then(res => {
      console.log(res);

      if (res.data.code === 0) {
        that.setData({
          photoList: res.data.data,
          nextPhotoPage: res.data.next_url
        })
      }

    })
  },


  // 用戶动态和相册切换事件
  Switch() {
    this.setData({
      cut: !this.data.cut
    })
    if (!this.data.cut) {
      this.getPhotolist()
    }
  },

  // 点击更多操作弹出底部弹框事件
  btnMore(e) {
    this.setData({
      Hireveal: true,
      show: e.currentTarget.dataset.status,
      statusId: e.currentTarget.dataset.id
    })

  },

  // 用户点击遮罩层隐藏底部弹窗事件 
  btnReveal() {
    this.setData({
      Hireveal: false,
      Hizfs: false
    })
  },

  // 用户动态转发事件
  btnForward(e) {
    let arr = this.data.UserStatuslist[e.currentTarget.dataset.index].img_list
    this.setData({
      Hizfs: true,
      arr,
      nickName: e.currentTarget.dataset.name,
      avatarUrl: e.currentTarget.dataset.url,
      qrcodeId: e.currentTarget.dataset.id,
      Index: e.currentTarget.dataset.index,
      Isown: e.currentTarget.dataset.isown
    })
  },

  // 跳转到海报页
  headleTransmit() {
    wx.navigateTo({
      url: '/pages/transmit/transmit?arr=' + JSON.stringify(this.data.arr) + '&nickName=' + this.data.nickName + '&avatarUrl=' + this.data.avatarUrl + '&qrcodeId=' + this.data.qrcodeId
    })
  },




  // 用户删除动态
  headleDelete() {
    let that = this
    wx.showLoading({
      title: '删除中...',
      mask: true
    })
    myRequesy({
      url: 'dynamic/delete',
      method: 'POST',
      data: {
        dynamic_id: that.data.statusId
      }
    }).then(res => {
      if (res.data.code === 0) {
        let {
          deleteId
        } = that.data
        deleteId.push(parseInt(that.data.statusId))
        that.setData({
          deleteId
        })
        wx.setStorageSync('deleteId', JSON.stringify(deleteId))
        that.getStatuslist()
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '已删除',
            })
          },
        })
        that.setData({
          Hireveal: false,
        })
      }
    })
  },

  // 用户取消置顶
  headleCancelTop() {
    let that = this
    myRequesy({
      url: 'dynamic/un-top',
      method: 'POST',
      data: {
        dynamic_id: that.data.statusId
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          Hireveal: false,
        })
        this.getStatuslist()
        wx.showToast({
          title: '取消成功',
          mask: true
        })
      }
    })
  },

  // 用户置顶动态
  headleTop() {
    let that = this
    myRequesy({
      url: 'dynamic/to-top',
      method: 'POST',
      data: {
        dynamic_id: that.data.statusId
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          Hireveal: false,
        })
        that.getStatuslist()
        wx.showToast({
          title: '置顶成功',
          mask: true
        })
      }

    })
  },

  //跳转到动态详情
  navComm(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/details/details?status=true&id=' + id,
    })

  },

  // 点击空白区域跳转到动态详情
  headleNavComm(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/details/details?&id=' + id,
    })
  },


  //获取用户基本信息
  getRestUser() {
    let that = this
    myRequesy({
      url: 'other-user/info',
      data: {
        user_id: that.data.userId
      }
    }).then(res => {
      if (res.data.code === 0) {
        that.setData({
          userInfo: res.data.data
        })
        let {
          listId
        } = that.data
        let obj = {
          id: res.data.data.id,
          follow_state: res.data.data.follow_state
        }
        if (listId.find(item => item.id == obj.id) !== undefined) {
          listId.splice(listId.findIndex(item1 => item1.id == obj.id), 1)
        }
        listId.push(obj)
        that.setData({
          listId
        })
        wx.setStorageSync('listId', listId)
      }
    })
  },

  // 跳转到动态详情页
  headleDetails(id) {
    console.log(id.detail);
    wx.navigateTo({
      url: '/pages/details/details?id=' + id.detail,
    })
  },

  //动态点赞
  headlePraise(e) {
    let {
      id,
      status,
      index
    } = e.currentTarget.dataset
    let that = this
    let {
      UserStatuslist
    } = that.data
    if (status) {
      myRequesy({
        url: 'dynamic-likes/delete',
        method: 'POST',
        data: {
          dynamic_id: id
        }
      }).then(res => {
        if (res.data.code === 0) {
          myRequesy({
            url: 'dynamic/detail',
            data: {
              dynamic_id: id
            }
          }).then(res => {
            if (res.data.code === 0) {
              UserStatuslist[index] = res.data.data
              that.setData({
                UserStatuslist
              })
              let {
                likes_count,
                is_likes,
                id,
                comments_count,
              } = res.data.data
              that.getTrigger(likes_count, id, comments_count, is_likes)
            }
          })
        }
      })
    } else {
      myRequesy({
        url: 'dynamic-likes/add',
        method: 'POST',
        data: {
          dynamic_id: id
        }
      }).then(res => {
        if (res.data.code === 0) {
          myRequesy({
            url: 'dynamic/detail',
            data: {
              dynamic_id: id
            }
          }).then(res => {
            if (res.data.code === 0) {
              UserStatuslist[index] = res.data.data
              that.setData({
                UserStatuslist
              })
              let {
                likes_count,
                comments_count,
                is_likes,
                id,
              } = res.data.data
              that.getTrigger(likes_count, id, comments_count, is_likes)
            }
          })
        }
      })
    }
  },

  //获取当前点击动态的状态
  getTrigger(likes_count, id, comments, is_likes, follow_state) {
    let that = this
    let {
      stateList
    } = that.data
    let obj = {
      likes_count,
      id,
      comments,
      is_likes,
      follow_state
    }
    if (stateList.find(item => item.id == id) !== undefined) {
      stateList.splice(stateList.findIndex(item => item.id == id), 1)
    }
    stateList.push(obj)
    this.setData({
      stateList
    })
    wx.setStorageSync('stateList', JSON.stringify(stateList))
  },

  //用户修改后的状态
  getSlist() {
    let arr = JSON.parse(wx.getStorageSync('stateList'))
    // let listId = wx.getStorageSync('listId')
    let {
      UserStatuslist
    } = this.data
    //判断缓存中的数据Id是否和data中的数据Id一样 点赞 评论  
    arr.find(item => {
      UserStatuslist.find(item1 => {
        if (item.id.toString() == item1.id.toString()) {
          item1.is_likes = item.is_likes
          item1.likes_count = item.likes_count
          item1.comments_count = item.comments
        }
      })
    })

    this.setData({
      UserStatuslist
    })
  },


  // 预览动态图片
  btnPreview(e) {
    let current = e.currentTarget.dataset.item1
    let urls = e.currentTarget.dataset.item
    wx.previewImage({
      current,
      urls
    })
  },


  // 跳转到标签页
  btnNavTag(e) {
    if (e.currentTarget.dataset.status) {
      wx.navigateTo({
        url: '/pages/hddetails/hddetails?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/tag/tag?id=' + e.currentTarget.dataset.id,
      })
    }
  },


  // 上拉获取动态下一页数据
  getBomStatuslist() {
    let that = this
    if (that.data.nextPage!== null) {
      that.data.pageNum+=1
      myRequesy({
        url: 'user/default-dynamic',
        data: {
          user_id: that.data.userId,
          page: that.data.pageNum
        }
      }).then(res => {
        if (res.data.code === 0) {
          that.setData({
            UserStatuslist: [...that.data.UserStatuslist, ...res.data.data],
            count: res.data.count,
            nextPage: res.data.next_url
          })
        }
      })
      return
    }
  },

  // 上拉获取照片下一页数据
  getBomPhotolist() {
    let that = this
    if (that.data.nextPhotoPage !== null) {
      myRequesy({
        url: 'user/album-dynamics',
        data: {
          user_id: that.data.userId,
          page: that.data.pagePhotoNum
        }
      }).then(res => {
        console.log(res);

        if (res.data.code === 0) {
          that.setData({
            photoList: [that.data.photoList, ...res.data.data],
            nextPhotoPage: res.data.next_url
          })
        }

      })
    }

  },

  // 跳转到投诉反馈页
  headleReport() {
    wx.navigateTo({
      url: '/pages/feedback/feedback?vid=' + this.data.userInfo.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (JSON.parse(wx.getStorageSync('deleteId') !== '')) {
      let deleteId = JSON.parse(wx.getStorageSync('deleteId'))
      this.setData({
        deleteId
      })
      let {
        UserStatuslist
      } = this.data
      UserStatuslist = UserStatuslist.filter(item => item.id != deleteId[0])
      this.setData({
        UserStatuslist
      })
    }

    // //获取缓存中的数据
    if (JSON.parse(wx.getStorageSync('stateList') !== '')) {
      this.getSlist()
    }

    if (wx.getStorageSync('listId') !== '') {
      let {
        userInfo
      } = this.data
      let listId = wx.getStorageSync('listId')
      listId.find(item => {
        if (item.id == userInfo.id) {
          userInfo.follow_state = item.follow_state
          this.setData({
            userInfo
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getBomStatuslist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from == "menu") {
      return {
        title: "一个记录美好的小程序，数千万摄影爱好者的聚集地",
        path: "/pages/dynamic/dynamic?id=" + this.data.userId
      }
    }
    return {
      title: this.data.Isown ? "我发布了一组作品，快来帮我点个赞" : "这组摄影作品很精彩，快来一起围观",
      path: "/pages/details/details?id=" + this.data.qrcodeId,
      imageUrl: this.data.UserStatuslist[this.data.Index].img_list[0]
    }
  }
})